package com.zoffy.ybuhelper.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.zoffy.ybuhelper.R;
import com.zoffy.ybuhelper.pojo.entity.Message;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GPTRecycleAdapter extends RecyclerView.Adapter<GPTRecycleAdapter.GPTHolder> {
    protected List<Message> dataList;
    public GPTRecycleAdapter(List<Message> dataList){
        this.dataList=dataList;
    }
    @NonNull
    @NotNull
    @Override
    public GPTHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message,parent,false);
        return new GPTHolder(view);
    }
    public void addLeft(Message message){
        dataList.add(0,message);
    }
    public void add(Message message){
        dataList.add(message);
    }
    public int size(){
        return dataList.size();
    }
    public Message get(int position){
        return dataList.get(position);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull GPTHolder holder, int position) {
        Message message= dataList.get(position);
        if(message.getOrigin()==0){
            holder.layoutOther.setVisibility(View.GONE);
            holder.messageMine.setText(message.getMessage());
        }
        else {
            holder.layoutMine.setVisibility(View.GONE);
            holder.messageOther.setText(message.getMessage());
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    protected class GPTHolder extends RecyclerView.ViewHolder {
        protected LinearLayout layoutOther, layoutMine;
        protected TextView myName, messageOther, messageMine;

        protected GPTHolder(View view) {
            super(view);
            layoutOther = view.findViewById(R.id.layout_other);
            layoutMine = view.findViewById(R.id.layout_mine);
            myName = view.findViewById(R.id.my_name);
            messageOther = view.findViewById(R.id.message_other);
            messageMine = view.findViewById(R.id.message_mine);
        }

    }
}
